package 序列化;

import java.io.*;

class Order implements Externalizable {
    private int code;
    private int id;
    private String tirtle;

    public Order() {
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTirtle() {
        return tirtle;
    }

    public void setTirtle(String tirtle) {
        this.tirtle = tirtle;
    }

    @Override
    public String toString() {
        return "Order{" +
                "code=" + code +
                ", id=" + id +
                ", tirtle='" + tirtle + '\'' +
                '}';
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeInt(code);
        System.out.println("writeExternal");
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        code=in.readInt();
        System.out.println("readExternal");
    }
}

public class TestSerializable02 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Order order=new Order();
        order.setCode(100);
        order.setId(20);
        ObjectOutputStream out=new ObjectOutputStream(
                new FileOutputStream("a1.txt"));
        out.writeObject(order);
        out.close();
        //反序列化
        ObjectInputStream in=new ObjectInputStream(
                new FileInputStream("a1.txt"));
        Object o=in.readObject();
        System.out.println(o);
        in.close();
    }
}
